package chaw.android.train;

import java.io.File;
import java.util.ArrayList;

import chaw.android.util.ImageSave;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

public class BoardPictureActivity extends Activity{
	private ImageView img1, img2, img3, img4, selected;
	private ImageView btn1, btn2, btn3, btn4, checked;
	private ImageView back;
	private String image1 = "", image2 = "", image3 = "", image4 = "", main = "";
	private int imageStat = 0;
	private ArrayList<String> image;
	static private Uri outputFileUri= Uri.EMPTY;
	static int selectedImageId;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
        setContentView(R.layout.board_picture);
        img1 = (ImageView)findViewById(R.id.img1);
        img2 = (ImageView)findViewById(R.id.img2);
        img3 = (ImageView)findViewById(R.id.img3);
        img4 = (ImageView)findViewById(R.id.img4);
        btn1 = (ImageView)findViewById(R.id.check1);
        btn2 = (ImageView)findViewById(R.id.check2);
        btn3 = (ImageView)findViewById(R.id.check3);
        btn4 = (ImageView)findViewById(R.id.check4);
        back = (ImageView)findViewById(R.id.back);
        image = new ArrayList<String>();
        checked = btn1;
        
        setClickListener();
        registerForContextMenu(img1);
        registerForContextMenu(img2);
        registerForContextMenu(img3);
        registerForContextMenu(img4);
	}
	
	public void setClickListener(){

		img1.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				selected = img1;
				selectedImageId = R.id.img1;
				openContextMenu(v);
			}
		});

		img2.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				selected = img2;
				selectedImageId = R.id.img2;
				openContextMenu(v);
			}
		});

		img3.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				selected = img3;
				selectedImageId = R.id.img3;
				openContextMenu(v);
			}
		});

		img4.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				selected = img4;
				selectedImageId = R.id.img4;
				openContextMenu(v);
			}
		});

		btn1.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				changeSelected(1);
			}
		});
		btn2.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				changeSelected(2);
			}
		});
		btn3.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				changeSelected(3);
			}
		});
		btn4.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				changeSelected(4);
			}
		});
		back.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getBaseContext(), BoardWriteActivity.class);
				if(image1 != ""){
					image.add(image1);
					if(checked.equals(btn1)){
						main = ""+image.size();
					}
				}
				if(image2 != ""){
					image.add(image2);
					if(checked.equals(btn2)){
						main = ""+image.size();
					}
				}
				if(image3 != ""){
					image.add(image3);
					if(checked.equals(btn3)){
						main = ""+image.size();
					}
				}
				if(image4 != ""){
					image.add(image4);
					if(checked.equals(btn4)){
						main = ""+image.size();
					}
				}
				if(image.isEmpty()){
					for(int i = 0 ; i < 4 ; i++){
						image.add("");
					}
					main = "";
				}
				int count = image.size();
				for(int i = 0 ; i < 4-count ; i++){
					image.add("");
				}
				intent.putExtra("image", image);
				intent.putExtra("main", main);
				setResult(RESULT_OK, intent);
				finish();
			}
		});

	}
	
	public void changeSelected(int input){
		switch(input){
		case 1:
			if(image1 != ""){
				checked = btn1;
				btn1.setImageResource(R.drawable.btn_photo_main);
				btn2.setImageResource(R.drawable.btn_photo_sub);
				btn3.setImageResource(R.drawable.btn_photo_sub);
				btn4.setImageResource(R.drawable.btn_photo_sub);
				main = image1;
			}
			break;
		case 2:
			if(image2 != ""){
				checked = btn2;
				btn2.setImageResource(R.drawable.btn_photo_main);
				btn1.setImageResource(R.drawable.btn_photo_sub);
				btn3.setImageResource(R.drawable.btn_photo_sub);
				btn4.setImageResource(R.drawable.btn_photo_sub);
				main = image2;
			}
			break;
		case 3:
			if(image3 != ""){
				checked = btn3;
				btn3.setImageResource(R.drawable.btn_photo_main);
				btn1.setImageResource(R.drawable.btn_photo_sub);
				btn2.setImageResource(R.drawable.btn_photo_sub);
				btn4.setImageResource(R.drawable.btn_photo_sub);
				main = image3;
			}
			break;
		case 4:
			if(image4 != ""){
				checked = btn4;
				btn4.setImageResource(R.drawable.btn_photo_main);
				btn1.setImageResource(R.drawable.btn_photo_sub);
				btn2.setImageResource(R.drawable.btn_photo_sub);
				btn3.setImageResource(R.drawable.btn_photo_sub);
				main = image4;
			}
			break;
		}
	}


	@Override
	public void onCreateContextMenu(ContextMenu menu ,View v, ContextMenu.ContextMenuInfo menuInfo){
		menu.add(Menu.NONE, 1, Menu.NONE, "사진촬영");
		menu.add(Menu.NONE, 2, Menu.NONE, "앨범에서 사진 선택");
		menu.add(Menu.NONE, 3, Menu.NONE, "지우기");
		menu.add(Menu.NONE, 4, Menu.NONE, "취소");
		super.onCreateContextMenu(menu, v, menuInfo);
	}

    @Override
    public boolean onContextItemSelected(MenuItem item) {
    	switch(item.getItemId()){
    	case 1:
			Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            String imagePath = String.valueOf(System.currentTimeMillis()) + "_tmp_image.jpg";
            File file = new File(ImageSave.getImageDir(), imagePath);
            outputFileUri = Uri.fromFile(file);
            camera.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
			startActivityForResult(camera, 1);
    		break;
    	case 2:
			Intent gallery = new Intent();
			gallery.setAction(Intent.ACTION_GET_CONTENT);
			gallery.setType("image/*");
			startActivityForResult(gallery, 2);
    		break;
    	case 3:
    		selected.setImageResource(R.drawable.icon_nophoto);
    		if(selected.equals(img1)){
   				image1 = "";
   			}
   			else if(selected.equals(img2)){
   				image2 = "";
   			}
   			else if(selected.equals(img3)){
   				image3 = "";
   			}
   			else if(selected.equals(img4)){
   				image4 = "";
   			}
    		break;
    	case 4:
    		break;
    	}
		return false;
    	
    }
    
    
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode == RESULT_OK){
			   if (requestCode == 1) {
		            Intent intent = new Intent("com.android.camera.action.CROP");
		            intent.setDataAndType(outputFileUri, "image/*");
		      
		            intent.putExtra("outputX", 300);
		            intent.putExtra("outputY", 300);
		            intent.putExtra("aspectX", 1);
		            intent.putExtra("aspectY", 1);
		            intent.putExtra("scale", true);
		            intent.putExtra("return-data", true);
		            startActivityForResult(intent, 3);
			   
			   } else if (requestCode == 2) {
				   if(data != null){
				   		try
				   		{
				   			outputFileUri = data.getData();

				   			imageStat = 2;
				   			
				            Intent intent = new Intent("com.android.camera.action.CROP");
				            intent.setDataAndType(outputFileUri, "image/*");
				      
				            intent.putExtra("outputX", 300);
				            intent.putExtra("outputY", 300);
				            intent.putExtra("aspectX", 1);
				            intent.putExtra("aspectY", 1);
				            intent.putExtra("scale", true);
				            intent.putExtra("return-data", true);
				            startActivityForResult(intent, 3);
				            
				   		}
				   		catch(Exception e){
				   			e.printStackTrace();
						}
				   }
			   }
			   else if (requestCode == 3) {
			        final Bundle extras = data.getExtras();
			        Bitmap photo = null;
				    if(extras != null){
					    try{
				        	photo = extras.getParcelable("data");
				        	
				            selected = (ImageView) findViewById(selectedImageId);
				            selected.setImageBitmap(photo);
				        	Bitmap cropImage = Bitmap.createScaledBitmap(photo, 640, 640, false); // 이미지 재설정
				   			String path = ImageSave.saveBitmap(cropImage, ImageSave.getImageDir());
				   			if(selected.equals(img1)){
				   				image1 = path;
				   			}
				   			else if(selected.equals(img2)){
				   				image2 = path;
				   			}
				   			else if(selected.equals(img3)){
				   				image3 = path;
				   			}
				   			else if(selected.equals(img4)){
				   				image4 = path;
				   			}
				   			if(imageStat == 1){
	//				            File f = new File(outputFileUri.getPath());
	//				            if (f.exists()) {
	//				                f.delete();
	//				            }
				   			}
				    	} catch (Exception e) {
							// TODO: handle exception
				    		e.printStackTrace();
						}
			        }
			   }
		}
	}
}
